package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.View
import client.global.commlib.comm_ui.ImageGalleryActivity
import client.global.commlib.dialog.CameraOrSelectDialog
import client.global.commlib.toast.toast
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.ActivityPersoninfoBinding
import com.kiln.juxingqs.dialog.EditUserNameDialog
import com.kiln.juxingqs.ui.vm.PersonInfoViewModel


class PersonInfoActivity : BaseMiddleActivity<PersonInfoViewModel, ActivityPersoninfoBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_personinfo
    }

    fun setReal() {
        startActivity(Intent(this, SetRealActivity::class.java))
    }

    override fun initView() {
        binding?.apply {
            event = this@PersonInfoActivity
            vm = viewModel
            emptyLayout.setOnClickListener { viewModel.userInfo() }
        }
    }

    fun editUserIk() {
        EditUserNameDialog(this).apply {
            ok = {
                viewModel.editName(it)
            }
        }.show()
    }

    override fun observerUI() {
        viewModel.callUser.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    binding.userInfo = it.data
                    if (it.data?.sex.equals("1")) {
                        binding.sex = "男"
                    } else {
                        binding.sex = "女"
                    }



//                    if (it.data?.status != 2) {
//                        binding.real.visibility = View.VISIBLE
//                    }


                    mImgLoader.load(it.data!!.avatar).error(R.mipmap.ic_launcher_round)
                        .into(binding.userAvatar)
                    mImgLoader.load(it.data!!.id_img)
                        .into(binding.idFontImg)


                    var realName: String = ""
                    when (it.data?.status) {
                        0 -> realName = "未审核"
                        1 -> realName = "审核中"
                        2 -> realName = "已实名"
                    }
                    binding.realnameVerifyTxt = realName

                    binding.emptyLayout.visibility = View.GONE
                }
            }
        })

        viewModel.callEditName.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("修改成功")
                    viewModel.userInfo()
                }
            }
        })
    }

    override fun onResume() {
        super.onResume()
        viewModel.userInfo()
    }

    override fun initViewModel(): PersonInfoViewModel = createViewModel()

    //选择上传头像
    fun selectAvatar() {
        CameraOrSelectDialog(this@PersonInfoActivity).apply {
            suc = {
                viewModel.userAvatar.value = it
                viewModel.setAvatar(this@PersonInfoActivity)
            }
        }.show()
    }

    //修改交易密码
    fun xgjymm() {
        startActivity(Intent(this@PersonInfoActivity, XgzjmmActivity::class.java))
    }

    //修改密码
    fun xgmm() {
        startActivity(Intent(this@PersonInfoActivity, XgmmActivity::class.java))
    }

    fun setskxx() {
//        startActivity(Intent(this@PersonInfoActivity, SetSkxxActivity::class.java))

        if (binding.userInfo?.hfb_pay_status == 1) {
            startActivity(Intent(this@PersonInfoActivity, LookSkxxNewActivity::class.java))
        } else {
            startActivity(Intent(this@PersonInfoActivity, SetSkxxNewActivity::class.java))
        }

    }

    fun goLook() {
        if (!binding.userInfo?.id_img.isNullOrEmpty())
            ImageGalleryActivity.show(
                this,
                binding.userInfo?.id_img,
                true,
                false
            )
    }
}